package com.rent.common.utils;

import java.util.Random;

/**
 * @Author zhang
 * @Create 2021/7/13 10:15
 */
public class StringUtils {

    public static String trim(String str) {
        return str == null ? null : str.trim();
    }

    public static boolean isEmpty(String str) {
        if (str == null || str.length() == 0) return true;
        str = str.trim();
        if (str.length() == 0) return true;
        return str.equalsIgnoreCase("null");
    }

    /**
     * 处理截取字符串
     */
    public static String getStringByHexMsg(String msg, int beginIndex, int endIndex) {
        if (isEmpty(msg)) {
            return "";
        }
        if (msg.length() >= endIndex * 2) {
            return msg.substring(beginIndex * 2, endIndex * 2);
        }
        return "";
    }

    /**
     * 处理截取字符串
     */
    public static String getStringByHexMsg(String msg, int beginIndex) {
        if (isEmpty(msg)) {
            return "";
        }
        if (msg.length() >= beginIndex * 2) {
            return msg.substring(beginIndex * 2);
        }
        return "";
    }

    //length用户要求产生字符串的长度
    public static String getRandomStr(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    //length用户要求产生纯数字字符串的长度
    public static String getRandomNumberStr(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(10);
            sb.append(number);
        }
        return sb.toString();
    }

    public static String getStrByIdLength(Integer id, int length) {
        if (id == null) return getRandomNumberStr(length);
        StringBuilder str = new StringBuilder(String.valueOf(id));
        int l = str.length();
        if (l == length) return str.toString();
        if (l > length) return str.substring(0, length);
        for (int i = 0; i < length; i++) {
            str.insert(0, "0");
        }
        return str.toString();
    }

    //可以指定字符串的某个位置是什么范围的值
    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(3);
            long result;
            switch (number) {
                case 0:
                    result = Math.round(Math.random() * 25 + 65);
                    sb.append((char) result);
                    break;
                case 1:
                    result = Math.round(Math.random() * 25 + 97);
                    sb.append((char) result);
                    break;
                case 2:
                    sb.append(new Random().nextInt(10));
                    break;
            }
        }
        return sb.toString();
    }


}
